package com.yanqu.road.server.cmd.passRoad;

import com.yanqu.road.entity.config.passroad.PassRoadPointInfo;
import com.yanqu.road.entity.player.UserPassRoad;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PassRoadPb;
import com.yanqu.road.pb.pass.RoadProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.passRoad.PassRoadModule;
import com.yanqu.road.server.gameplayer.module.player.CardModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.config.PassRoadMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.S_PASS_ROAD_BATTLE, desc = "新关卡战斗")
public class PassRoadBattleCmd extends AbstractCommand {


    private PassRoadPointInfo info;//当前要打的关卡的信息

    private boolean isEventSelect = false;
    //选择题选择的选项
    private int selectId;
    //派遣的门客列表
    private List<Integer> patronIdlist;
    //赔钱的美女列表
    private List<Integer> beautyIdList;

    private String eventReward = "";

    private boolean isBackend = false;

    private int maxSilverBuffTimes = 0;

    private int maxItemBuffTimes = 0;

    private int autoBuyBuffTimes = 0;

    private int autoItemBuyBuffTimes = 0;

    private int autoBuffOption = 2;//1银两优先 2道具优先

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RoadProto.RoadBattleReqMsg reqMsg = RoadProto.RoadBattleReqMsg.parseFrom(packet.getBytes());
        isEventSelect = reqMsg.getIsEventSelect();
        selectId = reqMsg.getSelectId();
        patronIdlist = reqMsg.getPatronIdList();
        beautyIdList = reqMsg.getBeautyIdList();
        isBackend = reqMsg.getIsBackend();
        maxSilverBuffTimes = reqMsg.getMaxSilverBuffTimes();
        maxItemBuffTimes = reqMsg.getMaxItemBuffTimes();
        autoBuffOption = reqMsg.getBuffOption();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        info = player.getModule(PassRoadModule.class).getPassPointInfo();

        if (isBackend) {
            //周卡特权，一键通关
            if(!player.getModule(CardModule.class).isPassRoadOneKeyPass()) {
                if (info.getId() <= GameConfig.ONE_KEY_BACKEND_PASS_ROAD_LIMIT
                        && VipMgr.getVipLv(player.getUserInfo().getVipExp()) < GameConfig.ONE_KEY_BACKEND_PASS_ROAD_VIP_LIMIT) {
                    ret = GameErrorCode.E_PASS_ROAD_BACKEND_NO_UNLOCK;
                    return;
                }
            }
        }

        eventReward = player.getModule(PassRoadModule.class).passEventReward(info.getId(), info.getSectionId(), info.getEventId(), selectId, patronIdlist, beautyIdList);
        ret = player.getModule(PassRoadModule.class).battle(isEventSelect, selectId, patronIdlist, beautyIdList, isBackend, maxSilverBuffTimes, maxItemBuffTimes,autoBuffOption);

        // 后台自动贸易停止原因（boss谈判失败时ret=0，需要另设）
        if (isBackend && ret != 0) {
            player.getModule(PassRoadModule.class).setBackEndStopReason(ret);
        }
        // 后台自动贸易自动鼓舞次数
        if (isBackend && ret == 0 && info.isBoss()) {
            Map<Integer, Integer> buffTimesMap = player.getModule(PassRoadModule.class).getAndResetAutoBuyBuffTimes();
            autoBuyBuffTimes = buffTimesMap.get(1);
            autoItemBuyBuffTimes = buffTimesMap.get(2);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        RoadProto.RoadBattleRespMsg.Builder respMsg = RoadProto.RoadBattleRespMsg.newBuilder();
        respMsg.setRet(ret);
        if (0 == ret) {
            UserPassRoad userPassRoad = player.getModule(PassRoadModule.class).getUserPassRoad();
            RoadProto.RoadTempMsg.Builder passMsg = PassRoadPb.parsePassRoadTempMsg(userPassRoad, autoBuyBuffTimes, autoItemBuyBuffTimes);
            if (null != passMsg) {
                respMsg.setPassData(passMsg);
            }
            respMsg.setReward(eventReward);
            Property reward = new Property(info.getReward());
            if (info.isBoss()) {
                reward.addProperty(PropertyHelper.parseStringToProperty(PassRoadMgr.getPassRoadInfo(info.getId()).getRewards()));
            }
            reward = player.getModule(PassRoadModule.class).getTransportAdd(reward);
            respMsg.setExperience(reward.getCountByGoodsId(GameConfig.GAME_MONEY_PATRONS_EXP).longValue());
        }
        player.sendPacket(Protocol.U_PASS_ROAD_BATTLE, respMsg);
    }
}
